import { defineConfig, loadEnv } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
// import electron from 'vite-plugin-electron'
import legacy from "@vitejs/plugin-legacy"
import fs from "fs"

import postCssPxToRem from "postcss-pxtorem"

import esbuild from "rollup-plugin-esbuild"
import 'core-js/features/array/flat-map'
import 'core-js/features/array/flat'

export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  const proxyPrefix = env.VITE_APP_PROXY_PREFIX
  return defineConfig({
    base: env.VITE_APP_BASE,
    define: {
      "process.env": {}
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()]
      }),
      Components({
        resolvers: [ElementPlusResolver()]
      }),
      esbuild({
        target: ["chrome52", "chrome64", "chrome66","chrome68","chrome71"],
        loaders: {
          ".vue": "js",
          ".ts": "js"
        }
      }),
      legacy({
        targets: ["defaults", "not IE 11"],
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
        renderLegacyChunks: true,
        polyfills: [
          "es.symbol",
          "es.promise",
          "es.promise.finally",
          "es/map",
          "es/set",
          "es.array.filter",
          "es.array.for-each",
          "es.array.flat-map",
          "es.object.define-properties",
          "es.object.define-property",
          "es.object.get-own-property-descriptor",
          "es.object.get-own-property-descriptors",
          "es.object.keys",
          "es.object.to-string",
          "web.dom-collections.for-each",
          "esnext.global-this",
          "esnext.string.match-all"
        ],
        modernPolyfills: [
          "es.promise.finally",
          "es.symbol",
          "es.promise",
          "es/map",
          "es/set",
          "es.array.filter",
          "es.array.for-each",
          "es.array.flat-map",
          "es.object.define-properties",
          "es.object.define-property",
          "es.object.get-own-property-descriptor",
          "es.object.get-own-property-descriptors",
          "es.object.keys",
          "es.object.to-string",
          "web.dom-collections.for-each",
          "esnext.global-this",
          "esnext.string.match-all"
        ]
      }),
      // basicSsl()
    ],
    css: {
      postcss: {
        plugins: [
          postCssPxToRem({
            rootValue: 216, // 设计稿尺寸 1rem大小
            propList: ["*"], // 需要转换的属性，这里选择全部都进行转换
            minPixelValue: 14 // 设置最小转换像素值
          })
        ]
      }
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src")
      }
    },

    build: {
      chunkSizeWarningLimit: 3000
    },
    server: {
      host: "0.0.0.0",
      port: env.VITE_APP_PORT || process.env.port,
      // https: true,
      https: {
        key: fs.readFileSync("certs/192.168.150.57+1-key.pem"),
        cert: fs.readFileSync("certs/192.168.150.57+1.pem")
      },
      proxy: {
        [proxyPrefix]: {
          target: env.VITE_APP_BASE_URL,
          changeOrigin: true,
          ws: true,
          toProxy: true,
          rewrite: (path) => path.replace(new RegExp(`^${proxyPrefix}`), "")
        }
      }
    }
  })
}
